package com.ikingtech.platform.service.system.user.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.platform.service.system.user.entity.UserTenantDO;
import com.ikingtech.platform.service.system.user.mapper.UserTenantMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangqiang
 */
@Slf4j
@Service
public class UserTenantRepository extends ServiceImpl<UserTenantMapper, UserTenantDO> {

    public void removeByTenantCode(String tenantCode) {
        this.remove(Wrappers.<UserTenantDO>lambdaQuery().eq(UserTenantDO::getTenantCode, tenantCode));
    }

    public void removeByUserId(String userId, String tenantCode) {
        this.remove(Wrappers.<UserTenantDO>lambdaQuery()
                .eq(UserTenantDO::getUserId, userId)
                .eq(Tools.Str.isNotBlank(tenantCode), UserTenantDO::getTenantCode, tenantCode));
    }

    public List<UserTenantDO> listByUserId(String userId) {
        return this.list(Wrappers.<UserTenantDO>lambdaQuery()
                .eq(UserTenantDO::getUserId, userId));
    }

    public List<String> listUserIdByTenantCode(String tenantCode) {
        return this.listObjs(Wrappers.<UserTenantDO>lambdaQuery()
                .select(UserTenantDO::getUserId)
                .eq(UserTenantDO::getTenantCode, tenantCode), String.class::cast);
    }

    public List<String> listUserIdByUserIdsAndTenantCode(List<String> userIds, String tenantCode) {
        return this.listObjs(Wrappers.<UserTenantDO>lambdaQuery()
                .select(UserTenantDO::getUserId)
                .in(UserTenantDO::getUserId, userIds)
                .eq(UserTenantDO::getTenantCode, tenantCode), String.class::cast);
    }

    public List<String> listTenantCodeByUserId(String userId) {
        return this.listObjs(Wrappers.<UserTenantDO>lambdaQuery()
                .select(UserTenantDO::getTenantCode)
                .eq(UserTenantDO::getUserId, userId), String.class::cast);
    }

    public String getTenantCodeByRecentLogin(boolean recentLogin) {
        return this.getObj(Wrappers.<UserTenantDO>lambdaQuery()
                .select(UserTenantDO::getTenantCode)
                .eq(UserTenantDO::getRecentLogin, recentLogin), String.class::cast);
    }
}
